package com.fare.taluo.fusionCharts;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.fare.taluo.util.FusionChartsUtil_2;

/**
 * 一个非常简易的报表产生工具
 * 测试阶段~~板砖轻拍
 * @author guokenye
 *
 */
public class SimpleFusionChartsBuilder {
	private  static SimpleFusionChartsBuilder builder=null;
	
	private SimpleFusionChartsBuilder() {}
	/**
	 * 获得工具对象
	 * @return
	 */
	public static SimpleFusionChartsBuilder getInstance(){
		if(builder==null){
			builder=new SimpleFusionChartsBuilder();
		}
		return builder;
	}
	/**
	 * 生成报表XML字符串，根据模版
	 * @param model
	 * @return
	 * @throws Exception 
	 */
	public String buildXMLStr(FusionChartsTemplateModel model) throws Exception{
		if(model==null){throw new Exception("Could Not Found FusionChartsTemplateModel");}
		StringBuffer sb=new StringBuffer("<?xml version='1.0' encoding='UTF-8'?>");//好像加个这个可以防止中文乱码
		//获得chart
		sb.append("<chart  ");
		Map<String,String> chart =model.getChart_Attr_Map();
		Iterator<Entry<String, String>> it=chart.entrySet().iterator();
		Entry<String, String> en;
		while(it.hasNext()){
			en=it.next();
			sb.append(en.getKey()+"='"+en.getValue()+"'  ");
		}
		sb.append(" > ");//标签补回去
		switch(model.getDefaultSeries()){//获得报表的系列
		case Single_Series://单系列
			sb.append(model.getSet_Single_FullStr());
			break;
		case Multi_Series://多系列
			sb.append("<categories> ");//X轴（横轴）
			if("".equals(model.getCategroyFullStr().toString())){
				 for (String s:model.getCategroyLabels()) {
					sb.append(" <category label='"+s+"' /> ");
				 }
			}else{
				sb.append(model.getCategroyFullStr().toString());
			}
			sb.append("</categories> ");
			//dataSet
			Map<Long, HashMap<String, String>> dataSets=model.getDataSet_Id_Attr_Map();
			Iterator<Entry<Long, HashMap<String, String>>> ds=dataSets.entrySet().iterator();
			Entry<Long, HashMap<String, String>> id_attr;
			while(ds.hasNext()){
				id_attr=ds.next();
				long id=id_attr.getKey();
				HashMap<String, String> attrs=id_attr.getValue();
				sb.append("<dataset ");
				Iterator<Entry<String, String>> i_attrs=attrs.entrySet().iterator();
				Entry<String, String> e_attrs;
				while(i_attrs.hasNext()){
					e_attrs=i_attrs.next();
					sb.append(e_attrs.getKey()+"='"+e_attrs.getValue()+"' ");
				}
				sb.append("> ");
				//获取对应 的set
				if(model.getSet_DataSetId_SetFullStr_Map().containsKey(id)){//存在完整set字符串
					sb.append(model.getSet_DataSetId_SetFullStr_Map().get(id));
				}else{
					List<String> values=model.getSet_DataSetId_SetValues_Map().get(id);
					for(String value:values){
						sb.append("<set value='"+value+"'/>");
					}
				}
				sb.append("</dataset> ");
			}
			break;
			default:
				throw new Exception("Could Not Found FusionCharts Series");
		}
		//添加style
		sb.append("<styles>");
		sb.append("<definition>");
		//这里比较麻烦，需要切割一下字符串
		Iterator<Entry<String, List<String>>> si=model.getToObject_Styles_Map().entrySet().iterator();
		Entry<String, List<String>> es;
		StringBuffer temp=new StringBuffer();
		while(si.hasNext()){
			es=si.next();
			String toObject=es.getKey();
			List<String> styles=es.getValue();
			for(String s:styles){
				s=s.toUpperCase();
				String name=findStyleName(s);
				sb.append(s);
				temp.append("<apply toObject='"+toObject+"' styles='"+name+"' />");
			}
		}
		sb.append("</definition>");
		if(!"".equals(temp.toString())){
			sb.append("<application>"+temp.toString()+"</application>");
		}
		sb.append("</styles>");
		sb.append("</chart>");
		return sb.toString();
	}
	/**
	 * 找出style的那么，如果没有返回“”
	 * @param style
	 * @return
	 */
	private static String findStyleName(String style){
		String name=null;
		int nameIndex=style==null?-1:style.indexOf("NAME");
		if(nameIndex<0){
			name="";
		}else{
			int start=style.indexOf("'",nameIndex)+1;
			int end=style.indexOf("'",start);
			name=style.substring(start,end);
		}
		
		return name;
	} 
	
	public static void main(String[] args) throws Exception {
		FusionChartsTemplateModel model=FusionChartsTemplateModel.getNewDefaultInstance();
		model.addChartAttr(FusionChartsTemplateModel.AN_Caption, "官网渠道走势对比图");
		List<String> dates=FusionChartsUtil_2.getDatesStr("2013-08-03", 5);
		model.addCategroyLabels(dates);
		model.createDataSet(1);
		model.addDataSetAttr(1,FusionChartsTemplateModel.AN_Seriesname, "CZ1234");
		String sets="<set value='100' toolText='大家好'/><set value='500' toolText='大家好'/>" +
				"<set value='200' toolText='大家好'/><set value='100' toolText='大家好'/>";
		model.addSetFullStrIntoDataSet(1, sets);
		model.addDataSetAttr(1, FusionChartsTemplateModel.AN_RenderAs, "Colum");
		//model.addDataSetAttr(1, "parentYAxis","S");
		model.createDataSet(2);
		String sets2="<set value='1003' toolText='大家好'/><set value='2500' toolText='大家好'/>" +
				"<set value='1200' toolText='大家好'/><set value='100' toolText='大家好'/>";
		model.addSetFullStrIntoDataSet(2, sets2);
		model.addDataSetAttr(2, FusionChartsTemplateModel.AN_RenderAs, "Line");
		//model.addDataSetAttr(2, "parentYAxis","P");
		//model.setSeries(Series.Single_Series);
		model.addStyleToObject("ToolTip", FusionChartsTemplateModel.STYLE_ToolTip_FontSize_15);
		model.addSetSingleFullStr("<set name='Web客户端用户登录' value='10'/><set name='客户端登录' value='12' />");
		
		String xml=SimpleFusionChartsBuilder.getInstance().buildXMLStr(model);
	}
}
